Census Data

Community
analysis
Rstudio
Published

February 28, 2022

Show the code
# Many many variables are included in the ACS. The ACS has 1 and 5 year estimates. Use the following code to see what variables are available:

# Set a year of interest
this.year = 2021

# This looks at the 5 year estimates
# You can also do "acs1"
vars <- load_variables(year = 2021,
                      dataset = "acs5",
                      cache = TRUE)

# There are 27886 possible variables 
dim(vars)
[1] 27886     4
Show the code
## Names for variable types
# Gives five year estimates

#OR

orDem <- get_acs(geography = "tract", year=this.year,
                  state = "OR", 
                  geometry = TRUE,
                  variables = c(popululation = "B02001_001",
                                median.gross.rent = "B25064_001",
                                median.household.income = "B19013_001",
                                rent.burden = "B25071_001",
                                white = "B03002_003", 
                                af.am = "B03002_004",
                                hispanic = "B03002_012",
                                am.ind = "B03002_005",
                                asian = "B03002_006",
                                nh.pi = "B03002_007",
                                multiple = "B03002_009",
                                other = "B03002_008"))

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  21%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |=================================================                     |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |=================================================================     |  94%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |======================================================================| 100%
Show the code
head(orDem)
Simple feature collection with 6 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -122.9903 ymin: 44.9539 xmax: -122.9767 ymax: 44.96986
Geodetic CRS:  NAD83
        GEOID                                      NAME     variable estimate
1 41047001607 Census Tract 16.07, Marion County, Oregon popululation     4442
2 41047001607 Census Tract 16.07, Marion County, Oregon        white     1359
3 41047001607 Census Tract 16.07, Marion County, Oregon        af.am        0
4 41047001607 Census Tract 16.07, Marion County, Oregon       am.ind       33
5 41047001607 Census Tract 16.07, Marion County, Oregon        asian      214
6 41047001607 Census Tract 16.07, Marion County, Oregon        nh.pi      982
  moe                       geometry
1 868 MULTIPOLYGON (((-122.9903 4...
2 600 MULTIPOLYGON (((-122.9903 4...
3  13 MULTIPOLYGON (((-122.9903 4...
4  56 MULTIPOLYGON (((-122.9903 4...
5 182 MULTIPOLYGON (((-122.9903 4...
6 465 MULTIPOLYGON (((-122.9903 4...
Show the code
## We’re going to need to do a little data wrangling so that we have a tidydata format to spread the column named ‘variable’.
orPct<-as.data.frame(orDem)[,c(1,3:4)]%>%
  spread(variable, estimate)%>%
  mutate(checkTot = white+af.am+hispanic+am.ind+ # looks good!
           asian+nh.pi+multiple+other)%>%
  mutate(pct.white = white/checkTot,
         pct.af.am = af.am/checkTot,
         pct.hispanic = hispanic/checkTot,
         pct.am.ind = am.ind/checkTot,
         pct.asian = asian/checkTot,
         pct.nh.pi = nh.pi/checkTot,
         pct.multiple = multiple/checkTot, 
         pct.other = other/checkTot, 
         year = this.year)

head(orPct)
        GEOID af.am am.ind asian hispanic median.gross.rent
1 41001950100    26     10     3      108               844
2 41001950200     9      9     9      106               706
3 41001950300    94     77     3       15               625
4 41001950400    15      0    27      223               787
5 41001950500    37     56     0      239               643
6 41001950600     6     83    16      105               731
  median.household.income multiple nh.pi other popululation rent.burden white
1                   51211       84    13     0         2579        27.0  2335
2                   45297      102     5     9         3332        34.8  3083
3                   42237       41    11     0         2845        35.4  2604
4                   52843        0     0     0         2829        30.6  2564
5                   54318      123     0     0         2579        29.5  2124
6                   48000       53     3     0         2375        26.1  2109
  checkTot pct.white   pct.af.am pct.hispanic  pct.am.ind   pct.asian
1     2579 0.9053897 0.010081427  0.041876696 0.003877472 0.001163242
2     3332 0.9252701 0.002701080  0.031812725 0.002701080 0.002701080
3     2845 0.9152900 0.033040422  0.005272408 0.027065026 0.001054482
4     2829 0.9063273 0.005302227  0.078826440 0.000000000 0.009544008
5     2579 0.8235750 0.014346646  0.092671578 0.021713843 0.000000000
6     2375 0.8880000 0.002526316  0.044210526 0.034947368 0.006736842
    pct.nh.pi pct.multiple  pct.other year
1 0.005040713   0.03257076 0.00000000 2021
2 0.001500600   0.03061224 0.00270108 2021
3 0.003866432   0.01441125 0.00000000 2021
4 0.000000000   0.00000000 0.00000000 2021
5 0.000000000   0.04769290 0.00000000 2021
6 0.001263158   0.02231579 0.00000000 2021
Show the code
# Set a year first
this.year = 2021

### BASE PLOT EXAMPLE
### Washington
or_tracts <- tracts(state = 'OR', 
                    cb = T, year = this.year)

# This is the structure of spatial data 
head(or_tracts)
Simple feature collection with 6 features and 13 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -122.9903 ymin: 44.88572 xmax: -118.7871 ymax: 45.7816
Geodetic CRS:  NAD83
  STATEFP COUNTYFP TRACTCE             AFFGEOID       GEOID   NAME
1      41      047  001607 1400000US41047001607 41047001607  16.07
2      41      005  980000 1400000US41005980000 41005980000   9800
3      41      051  001301 1400000US41051001301 41051001301  13.01
4      41      067  031516 1400000US41067031516 41067031516 315.16
5      41      059  950500 1400000US41059950500 41059950500   9505
6      41      051  005103 1400000US41051005103 41051005103  51.03
             NAMELSAD STUSPS        NAMELSADCO STATE_NAME LSAD      ALAND
1  Census Tract 16.07     OR     Marion County     Oregon   CT    1814790
2   Census Tract 9800     OR  Clackamas County     Oregon   CT 1634590508
3  Census Tract 13.01     OR  Multnomah County     Oregon   CT     744911
4 Census Tract 315.16     OR Washington County     Oregon   CT    1879977
5   Census Tract 9505     OR   Umatilla County     Oregon   CT 1073211610
6  Census Tract 51.03     OR  Multnomah County     Oregon   CT     433965
   AWATER                       geometry
1       0 MULTIPOLYGON (((-122.9903 4...
2 6684470 MULTIPOLYGON (((-122.266 45...
3       0 MULTIPOLYGON (((-122.6348 4...
4       0 MULTIPOLYGON (((-122.8068 4...
5       0 MULTIPOLYGON (((-119.4346 4...
6  155315 MULTIPOLYGON (((-122.6776 4...
Show the code
plot(or_tracts)

Show the code
# GGPLOT
ggplot(or_tracts) + 
  geom_sf() + 
  coord_sf()

Show the code
### GET CENSUS DATA
### B25077_001E: MEDIAN HOME VALUE
or <- get_acs(geography = "tract", year=this.year,
              state = "OR", 
              variables = "B25077_001E")%>%
  mutate(GEO_ID=paste0("1400000US", GEOID))

head(or)
# A tibble: 6 × 6
  GEOID       NAME                                  varia…¹ estim…²   moe GEO_ID
  <chr>       <chr>                                 <chr>     <dbl> <dbl> <chr> 
1 41001950100 Census Tract 9501, Baker County, Ore… B25077…  265000 46736 14000…
2 41001950200 Census Tract 9502, Baker County, Ore… B25077…  175200 40020 14000…
3 41001950300 Census Tract 9503, Baker County, Ore… B25077…  140600 22155 14000…
4 41001950400 Census Tract 9504, Baker County, Ore… B25077…  190500 36472 14000…
5 41001950500 Census Tract 9505, Baker County, Ore… B25077…  191100 11383 14000…
6 41001950600 Census Tract 9506, Baker County, Ore… B25077…  237500 46978 14000…
# … with abbreviated variable names ¹​variable, ²​estimate
Show the code
## USE GEO_JOIN TO COMBINE SPATIAL DATA AND OTHER DATA FRAMES

joinOR<- geo_join(or_tracts, or, 
                 by_sp="GEOID", by_df="GEOID")

## USE TMAP PACKAGE
tm_shape(joinOR)+
  tm_fill("estimate", style = "quantile", n=7, palette = "Greens")+
  tm_legend(bg.color="white", bg.alpha=0.6)+
  tm_style("gray")

Show the code
## SET GEOMETRY
oregon <- get_acs(geography = "tract", year=this.year,
               state = "OR",
               variables = "B25077_001E",
               geometry = TRUE)

#Plotting with MAPVIEW 
mapview(oregon, zcol = "estimate", legend = TRUE, 
        lwd=.25)
Show the code
## Plotting with LEAFLET

## Leaflet with reactive
pal<-colorNumeric("Greens", domain=0:ceiling(max(oregon$estimate, na.rm=TRUE)))
popup<-paste("Tract: ", as.character(substring(oregon$GEOID, 6, 11)), "<br>",
             "Median Home Value: ", as.character(oregon$estimate))
leaflet()%>%
  addProviderTiles("CartoDB.Positron")%>%
  addPolygons(data=oregon,
              fillColor= ~pal(oregon$estimate),
              fillOpacity = 0.7,
              weight = 0.4,
              smoothFactor = 0.2,
              popup = popup)